﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows;
using System.Windows.Forms;

namespace FanaticGene.WallPaperUtil
{
    public static class WallPaperHelper
    {
        /// <summary>
        /// 将 WPF 窗体投放到桌面上。
        /// </summary>
        /// <param name="window">WPF 窗体对象。</param>
        public static void Apply(Window window)
        {
            try
            {
                int handle = new WindowInteropHelper(window).Handle.ToInt32();
                User32.ApplyHandle(handle);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
           
        }

        /// <summary>
        /// 将 WinForms 窗体投放到桌面上。
        /// </summary>
        /// <param name="window">WinForms 窗体对象。</param>
        public static void Apply(Form form)
        {
            try
            {
                int handle = form.Handle.ToInt32() ;
                User32.ApplyHandle(handle);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        /// <summary>
        /// 重新更新桌面壁纸。
        /// </summary>
        public static void Refresh()
        {
            User32.Refresh();
        }

        /// <summary>
        /// 修改壁纸。
        /// </summary>
        /// <param name="filePath">图片的位置。</param>
        public static void ChangeWallPaper(string filePath)
        {
            User32.ChangeWallPaper(filePath);
        }
    }
}
